extends Card

# 汲元术卡牌
# 功能：贷款X点能量，抽X张牌

@export var energy_loan: int = 3  # 贷款的能量数量，可在tres文件中修改

func get_default_tooltip() -> String:
	# 使用%操作符替换tooltip_text中的占位符
	return tooltip_text % [energy_loan, energy_loan]

func get_updated_tooltip(_player_modifiers: ModifierHandler, _enemy_modifiers: ModifierHandler) -> String:
	# 使用%操作符替换tooltip_text中的占位符
	return tooltip_text % [energy_loan, energy_loan]

func apply_effects(targets: Array[Node], modifiers: ModifierHandler) -> void:
	print("👻 汲元术卡牌被使用！")
	
	var player = null
	var tree = null
	var player_handler = null
	
	# 通过targets参数获取场景树
	if targets.size() > 0:
		tree = targets[0].get_tree()
		if tree:
			player = Card.find_player(tree)
		else:
			print("无法获取场景树")
			return
	else:
		print("没有目标，无法执行效果")
		return
	
	if player and player.stats:
		# 记录旧的贷款值
		var old_new_loan = player.stats.new_loan_mana
		
		# 贷款能量：增加new_loan_mana
		player.stats.new_loan_mana += energy_loan
		print("汲元术贷款能量: new_loan_mana %d → %d (+%d)" % [old_new_loan, player.stats.new_loan_mana, energy_loan])
		
		# 抽牌：使用Card基类的静态方法查找player_handler
		player_handler = Card.find_player_handler(tree)
		
		if player_handler:
			player_handler.draw_cards(energy_loan)
			print("汲元术抽牌: 抽取 %d 张牌" % energy_loan)
		else:
			print("❌ 无法找到player_handler，无法抽牌")
			print("  - 尝试通过组查找: %s" % ("成功" if tree.get_first_node_in_group("player_handler") else "失败"))
			print("  - 尝试通过battle查找: %s" % ("成功" if tree.get_first_node_in_group("battle") else "失败"))
			print("  - 尝试通过player父节点查找: %s" % ("成功" if player.get_parent() else "失败"))
		
		# 播放音效
		if sound:
			SFXPlayer.play(sound)
	else:
		print("无法获取玩家或玩家状态") 
